package com.hmall.trade.listener;

import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class PayStatusLinstener {
    private final IOrderService orderService;

    @RabbitListener(bindings = @QueueBinding(
            value= @Queue("trade.pay.success.queue"),
            exchange = @Exchange(value = "pay.topic",type = "direct"),
            key = {"pay.success"}
    ))
    public void listenPaySucess(Long  orderId){
        log.info("成功接收支付成功的消息，交易订单id:{}",orderId);
        Order order = orderService.getById(orderId);
        if(order == null || order.getStatus() != 1){
            //如果订单不是未支付状态，说明消息重复消费了，则直接ack
            return ;
        }
        orderService.markOrderPaySuccess(orderId);
    }
}
